Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Z
Zsirozas
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sillinger Péter
Zsirozas
Commits
e96e80f1
Commit
e96e80f1
authored
4 years ago
by
Sillinger Péter
Browse files
Options
Downloads
Patches
Plain Diff
Extract merge the listening thread into RawTcpRemoteServer.
parent
570064e3
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
zsirozas/Form1.cs
+3
-1
3 additions, 1 deletion
zsirozas/Form1.cs
zsirozas/GameClient.cs
+7
-94
7 additions, 94 deletions
zsirozas/GameClient.cs
zsirozas/RawTcpTransport.cs
+104
-4
104 additions, 4 deletions
zsirozas/RawTcpTransport.cs
zsirozas/SignalR-Transport.cs
+1
-1
1 addition, 1 deletion
zsirozas/SignalR-Transport.cs
with
115 additions
and
100 deletions
zsirozas/Form1.cs
+
3
−
1
View file @
e96e80f1
...
...
@@ -210,6 +210,7 @@ namespace zsirozas
{
var
rooms
=
client
.
remoteServer
.
ListRooms
();
var
roomInfos
=
new
Dictionary
<
string
,
RoomInfo
>();
listBoxRooms
.
Items
.
Clear
();
foreach
(
var
roomID
in
rooms
)
{
listBoxRooms
.
Items
.
Add
(
roomID
);
...
...
@@ -217,6 +218,7 @@ namespace zsirozas
}
var
users
=
client
.
remoteServer
.
ListUsers
();
var
userInfos
=
new
Dictionary
<
string
,
UserInfo
>();
listBoxUsers
.
Items
.
Clear
();
foreach
(
var
userID
in
users
)
{
listBoxUsers
.
Items
.
Add
(
userID
);
...
...
@@ -327,7 +329,7 @@ namespace zsirozas
private
void
Form1_FormClosing
(
object
sender
,
FormClosingEventArgs
e
)
{
if
(
client
.
server
!=
null
)
if
(
client
.
server
Addr
!=
null
)
{
try
{
...
...
This diff is collapsed.
Click to expand it.
zsirozas/GameClient.cs
+
7
−
94
View file @
e96e80f1
...
...
@@ -58,18 +58,16 @@ namespace zsirozas
{
if
(
pingServer
(
server_ip
,
server_port
))
{
server
=
new
IPEndPoint
(
IPAddress
.
Parse
(
server_ip
),
server_port
);
server
Addr
=
new
IPEndPoint
(
IPAddress
.
Parse
(
server_ip
),
server_port
);
try
{
remoteServer
=
new
RawTcpRemoteServer
(
server
.
Address
,
server
.
Port
);
incomingListener
=
new
TcpListener
(
IPAddress
.
Any
,
serverIncoming
);
incomingListener
.
Start
();
remoteServer
=
new
RawTcpRemoteServer
(
serverAddr
.
Address
,
serverAddr
.
Port
,
this
,
IPAddress
.
Any
,
serverIncoming
);
//if (SendRequest(ServerAction.Login, nickname + "\n" + ((IPEndPoint)incomingListener.Server.LocalEndPoint).Port))
userID
=
remoteServer
.
CreateUser
(
nickname
,
ConnectionType
.
RawTCP
,
((
IPEndPoint
)
incomingListener
.
Server
.
LocalEndPoint
).
Port
);
//TODO: ez így ronda!
userID
=
remoteServer
.
CreateUser
(
nickname
,
ConnectionType
.
RawTCP
,
((
IPEndPoint
)
(
remoteServer
as
RawTcpRemoteServer
).
incomingListener
.
Server
.
LocalEndPoint
).
Port
);
if
(!
string
.
IsNullOrEmpty
(
userID
))
{
messageStream
=
incomingListener
.
AcceptTcpClient
().
GetStream
();
new
Thread
(
ListenForServer
)
{
IsBackground
=
true
}.
Start
();
if
(
remoteServer
.
ServerInfo
().
bigRoom
)
{
roomID
=
remoteServer
.
ListRooms
()[
0
];
...
...
@@ -90,17 +88,15 @@ namespace zsirozas
// generally talking to server
//
public
event
EventHandler
<
string
>
HandleServerError
=
(
sender
,
e
)
=>
{
};
public
IPEndPoint
server
;
public
IPEndPoint
server
Addr
;
public
string
userID
;
public
string
roomID
;
public
string
gameID
;
public
int
playerIDX
;
TcpListener
incomingListener
=
null
;
Stream
messageStream
=
null
;
public
IServerApiProvider
remoteServer
;
public
bool
StartGame
()
...
...
@@ -123,89 +119,6 @@ namespace zsirozas
remoteServer
.
JoinRoom
(
userID
,
roomID
);
}
void
ListenForServer
()
{
using
(
var
sr
=
new
StreamReader
(
messageStream
))
{
while
(
true
)
{
try
{
ParseNextMessage
(
sr
);
}
catch
(
Exception
ex
)
{
//TODO: rossz debug módszer....
HandleServerError
(
this
,
ex
.
Message
);
System
.
Diagnostics
.
Debugger
.
Break
();
return
;
}
}
}
//using (var sr = new StreamReader(messageStream))
//{
// string line = "";
// while (true)
// {
// try
// {
// while (string.IsNullOrEmpty(line))
// {
// line = sr.ReadLine();
// }
// var @event = (ServerEvent)Enum.Parse(typeof(ServerEvent), line);
// switch (@event)
// {
// case ServerEvent.GameEvent:
// //ToODO: validálni
// line = sr.ReadLine();
// GameEvent gameEvent;
// Enum.TryParse<GameEvent>(line, out gameEvent);
// switch (gameEvent)
// {
// case GameEvent.NewTurn:
// gameState = new JavaScriptSerializer().Deserialize<MidGameState>(sr.ReadLine());
// //ToODO: ezt majd valahol máshol nézegetni...
// playerIDX = Array.IndexOf<string>(gameState.playersByOrder, userID);
// gameStateCanged(this, gameEvent);
// //SendRequest(ServerAction.GameAction, PlayerAction.GetCards + "\n0");
// remoteServer.GameAction(gameID, userID, PlayerAction.GetCards, 0); cardsChanged();
// break;
// case GameEvent.CardAction:
// gameState = new JavaScriptSerializer().Deserialize<MidGameState>(sr.ReadLine());
// gameStateCanged(this, gameEvent);
// break;
// case GameEvent.GameOver:
// string msg = sr.ReadLine();
// var dummy = new { ID = "", score = 0 };
// var scores = new JavaScriptSerializer()
// .Deserialize<IEnumerable<object>>(msg)
// .Select((arg) => CastByExample(dummy, arg));
// msg = string.Join("\n", scores.Select((arg) => arg.ID + "\t" + arg.score).ToArray());
// gameOver(this, "A játéknak vége!\n" + msg);
// break;
// default:
// break;
// }
// break;
// case ServerEvent.Logout:
// return;
// default:
// break;
// }
// }
// catch (Exception)
// {
// line = sr.ReadLine();
// continue;
// }
// }
//}
}
public
void
SimpleNotify
(
ServerEvent
@event
,
string
param0
)
{
...
...
This diff is collapsed.
Click to expand it.
zsirozas/RawTcpTransport.cs
+
104
−
4
View file @
e96e80f1
...
...
@@ -5,6 +5,7 @@ using System.IO;
using
System.Net
;
using
System.Web.Script.Serialization
;
using
System.Linq
;
using
System.Threading
;
namespace
zsirozas
{
...
...
@@ -77,12 +78,105 @@ namespace zsirozas
/// </summary>
class
RawTcpRemoteServer
:
IServerApiProvider
{
public
RawTcpRemoteServer
(
IPAddress
IP
,
int
port
)
IAppClient
client
;
//incoming message loop
void
ListenForServer
()
{
using
(
var
sr
=
new
StreamReader
(
messageStream
))
{
while
(
true
)
{
try
{
server
=
new
IPEndPoint
(
IP
,
port
);
client
.
ParseNextMessage
(
sr
);
}
public
IPEndPoint
server
;
catch
(
Exception
ex
)
{
Console
.
WriteLine
(
ex
.
StackTrace
);
System
.
Diagnostics
.
Debugger
.
Break
();
return
;
}
}
}
//using (var sr = new StreamReader(messageStream))
//{
// string line = "";
// while (true)
// {
// try
// {
// while (string.IsNullOrEmpty(line))
// {
// line = sr.ReadLine();
// }
// var @event = (ServerEvent)Enum.Parse(typeof(ServerEvent), line);
// switch (@event)
// {
// case ServerEvent.GameEvent:
// //ToODO: validálni
// line = sr.ReadLine();
// GameEvent gameEvent;
// Enum.TryParse<GameEvent>(line, out gameEvent);
// switch (gameEvent)
// {
// case GameEvent.NewTurn:
// gameState = new JavaScriptSerializer().Deserialize<MidGameState>(sr.ReadLine());
// //ToODO: ezt majd valahol máshol nézegetni...
// playerIDX = Array.IndexOf<string>(gameState.playersByOrder, userID);
// gameStateCanged(this, gameEvent);
// //SendRequest(ServerAction.GameAction, PlayerAction.GetCards + "\n0");
// remoteServer.GameAction(gameID, userID, PlayerAction.GetCards, 0); cardsChanged();
// break;
// case GameEvent.CardAction:
// gameState = new JavaScriptSerializer().Deserialize<MidGameState>(sr.ReadLine());
// gameStateCanged(this, gameEvent);
// break;
// case GameEvent.GameOver:
// string msg = sr.ReadLine();
// var dummy = new { ID = "", score = 0 };
// var scores = new JavaScriptSerializer()
// .Deserialize<IEnumerable<object>>(msg)
// .Select((arg) => CastByExample(dummy, arg));
// msg = string.Join("\n", scores.Select((arg) => arg.ID + "\t" + arg.score).ToArray());
// gameOver(this, "A játéknak vége!\n" + msg);
// break;
// default:
// break;
// }
// break;
// case ServerEvent.Logout:
// return;
// default:
// break;
// }
// }
// catch (Exception)
// {
// line = sr.ReadLine();
// continue;
// }
// }
//}
}
public
RawTcpRemoteServer
(
IPAddress
serverIP
,
int
serverPort
,
IAppClient
client
,
IPAddress
listenIP
,
int
listenPort
)
{
this
.
client
=
client
;
server
=
new
IPEndPoint
(
serverIP
,
serverPort
);
incomingListener
=
new
TcpListener
(
listenIP
,
listenPort
);
incomingListener
.
Start
();
}
public
IPEndPoint
server
;
public
TcpListener
incomingListener
;
public
Stream
messageStream
=
null
;
//
//Implemetation of the server API
//
...
...
@@ -146,6 +240,12 @@ namespace zsirozas
userID
=
sr
.
ReadLine
();
}
}
//initialize the incoming message loop
messageStream
=
incomingListener
.
AcceptTcpClient
().
GetStream
();
new
Thread
(
ListenForServer
)
{
IsBackground
=
true
}.
Start
();
return
userID
;
}
}
...
...
@@ -216,7 +316,7 @@ namespace zsirozas
throw
new
ServerErrorException
(
status
);
}
status
=
sr
.
ReadLine
();
return
status
.
Split
(
' '
).
ToArray
();
return
status
.
Split
(
' '
).
Where
(
s
=>
!
string
.
IsNullOrEmpty
(
s
)).
ToArray
();
}
}
}
...
...
This diff is collapsed.
Click to expand it.
zsirozas/SignalR-Transport.cs
+
1
−
1
View file @
e96e80f1
...
...
@@ -17,7 +17,7 @@ namespace zsirozas
{
this
.
client
=
client
;
hub
=
new
HubConnection
(
server_url
).
CreateHubProxy
(
nameof
(
SignalRServerHub
));
//proxy calls from srver
//proxy calls from s
e
rver
hub
.
On
<
ServerEvent
,
string
>(
nameof
(
client
.
SimpleNotify
),
client
.
SimpleNotify
);
hub
.
On
<
GameEvent
,
string
>(
nameof
(
client
.
InGameNotify
),
client
.
InGameNotify
);
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment